<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
header('Content-type: text/html; charset=utf-8');
$link = mysql_connect("localhost", "root", "laropavieja.net.co") or die(mysql_error());
//$link = mysql_connect("localhost", "root", "ornitorrinco666") or die(mysql_error());
mysql_query("SET NAMES 'utf8'");
mysql_select_db("corpoica", $link) or die(mysql_error());

$str = "<table border=1 style=\"font-size:small \"><tr><td>Correo</td><td>Nombre</td><td>MUNICIPIO</td><td>Departamento</td></tr>";






$sql1 = "SELECT a.id, a.username,a.razon_social, a.branch_id, a.email, a.finalizado, c.nombre AS Municipio, d.nombre AS Departamento
FROM users a
LEFT JOIN user_cities b ON a.id = b.user_id
LEFT JOIN cities c ON c.id = b.city_id
LEFT JOIN departaments d ON d.id = c.Departament_id
WHERE a.branch_id =2";
$res1 = mysql_query($sql1, $link) or die(mysql_error());
$cont = 0;
$nombre="";
while ($row2 = mysql_fetch_array($res1)) {
    if($nombre!=$row2 ['razon_social']){
        $cont++;
    }
    
    $str.="<tr><td>" . $row2 ['email'] . "</td><td>".$row2 ['razon_social'] ."</td><td>".$row2 ['Municipio']."</td><td>".$row2 ['Departamento']. "</td></tr>";

    
}




$str.="</table>";
echo $str;

echo "Total cpga = $cont."
?>
